function formatDate(date) {
    if (date) {
        return `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    }
    return "";
}

function formatDate3(date) {
    if (date) {
        return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    }
    return "";
}

function getDate(originVal) {
    if (originVal) {
        // const dt = new Date(originVal)
        if (typeof originVal == "string") {
            const datetime = originVal.replace(/\-/g, "/")
            const dt = new Date(datetime)
            return _getDate(dt);
        } else {
            return _getDate(originVal);
        }
    }
}

function _getDate(date) {
    const y = date.getFullYear()
    const m = (date.getMonth() + 1 + '').padStart(2, '0')
    const d = (date.getDate() + '').padStart(2, '0')
    return `${y}-${m}-${d}`
}

function getDateTime(originVal) {
    if (originVal) {
        const datetime = originVal.replace(/\-/g, "/")
        const dt = new Date(datetime)
            // const dt = new Date(originVal)
        const y = dt.getFullYear()
        const m = (dt.getMonth() + 1 + '').padStart(2, '0')
        const d = (dt.getDate() + '').padStart(2, '0')
        const hh = (dt.getHours() + '').padStart(2, '0')
        const mm = (dt.getMinutes() + '').padStart(2, '0')
        const ss = (dt.getSeconds() + '').padStart(2, '0')
        return `${y}-${m}-${d} ${hh}:${mm}`
    }
    return "";
}

function getDateTimeSecond(originVal) {
    if (originVal) {
        const datetime = originVal.replace(/\-/g, "/")
        const dt = new Date(datetime)
            // const dt = new Date(originVal)
        const y = dt.getFullYear()
        const m = (dt.getMonth() + 1 + '').padStart(2, '0')
        const d = (dt.getDate() + '').padStart(2, '0')
        const hh = (dt.getHours() + '').padStart(2, '0')
        const mm = (dt.getMinutes() + '').padStart(2, '0')
        const ss = (dt.getSeconds() + '').padStart(2, '0')
        return `${y}${m}${d}${hh}${mm}${ss}`
    }
    return "";
}

function getClock(originVal) {
    const datetime = originVal.replace(/\-/g, "/")
    const dt = new Date(datetime)
        // const dt = new Date(originVal)
    const y = dt.getFullYear()
    const m = (dt.getMonth() + 1 + '').padStart(2, '0')
    const d = (dt.getDate() + '').padStart(2, '0')
    const hh = (dt.getHours() + '').padStart(2, '0')
    const mm = (dt.getMinutes() + '').padStart(2, '0')
    const ss = (dt.getSeconds() + '').padStart(2, '0')
    return `${hh}:${mm}`
}

function getTime(originVal) {

    const dt = new Date(originVal)
    const y = dt.getFullYear()
    const m = (dt.getMonth() + 1 + '').padStart(2, '0')
    const d = (dt.getDate() + '').padStart(2, '0')
    const hh = (dt.getHours() + '').padStart(2, '0')
    const mm = (dt.getMinutes() + '').padStart(2, '0')
    const ss = (dt.getSeconds() + '').padStart(2, '0')
    return `${y}-${m}-${d}`
}

function dateAdd(interval, number, date) {
    switch (interval) {
        case "y":
            {
                var year = date.getFullYear() + number;
                date.setFullYear(year);
                return date;
            }
        case "q":
            {
                date.setMonth(date.getMonth() + number * 3);
                return date;
            }
        case "m":
            {
                date.setMonth(date.getMonth() + number);
                return date;
            }
        case "w":
            {
                date.setDate(date.getDate() + number * 7);
                return date;
            }
        case "d":
            {
                date.setDate(date.getDate() + number);
                return date;
            }
        case "h":
            {
                date.setHours(date.getHours() + number);
                return date;
            }
        case "m":
            {
                date.setMinutes(date.getMinutes() + number);
                return date;
            }
        case "s":
            {
                date.setSeconds(date.getSeconds() + number);
                return date;
            }
        default:
            {
                date.setDate(date.getDate() + number);
                return date;
            }
    }
}

function formatDate2(date) {
    if (date) {
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let hour = date.getHours();
        let minute = date.getMinutes();
        if (month >= 1 && month <= 9) {
            month = `0${month}`;
        }
        if (day >= 1 && day <= 9) {
            day = `0${day}`;
        }
        if (hour >= 0 && hour <= 9) {
            hour = `0${hour}`;
        }
        if (minute >= 0 && minute <= 9) {
            minute = `0${minute}`;
        }
        return `${year}-${month}-${day} ${hour}:${minute}`
    }
    return "";
}

function addDate(date, days) {
    var date = new Date(date);
    date.setDate(date.getDate() + days);
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var mm = "'" + month + "'";
    var dd = "'" + day + "'";
    //��λ��ǰ���0
    if (mm.length == 3) {
        month = "0" + month;
    }
    if (dd.length == 3) {
        day = "0" + day;
    }
    var time = date.getFullYear() + "-" + month + "-" + day
    return time;
}

function getDateDiffs(days) {
    var tag = "";
    if (days < 0) {
        tag = "剩";
        days = -days;
    }
    var yearLevelValue = 365;
    var monthLevelValue = 31;
    var year = parseInt((parseInt(days)) / yearLevelValue);
    var month = parseInt((days - year * yearLevelValue) / monthLevelValue);
    var day = parseInt(days - year * yearLevelValue - month * monthLevelValue);
    var result = "";
    if (year != 0) result = result + year + "年";
    if (month != 0) result = result + month + "月";
    if (day != 0)
        result = result + day + "天";
    return tag + result;
}

export { formatDate, getDate, getDateTime, getClock, getTime, dateAdd, formatDate2, formatDate3, addDate, getDateDiffs };